﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing

Public Class ClsMMSystemBoundaryNode
    Inherits ContainerNode
    'represents system boundary node in use case diagram

#Region "Attributes"
#Region "Private attributes"
    Private MiID As Integer = -1 'MY OWN ID for this node. ID identifies this node - useful during synchroselection in compare version mode
    Private MbHasRTFnote As Boolean = False 'unit RTF note indicator
#End Region
#End Region
#Region "Constructors"
    Public Sub New()
        MyBase.new()
        AddID()
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal prototype As MindFusion.Diagramming.ContainerNode)
        MyBase.New()
        AddID()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram)
        MyBase.New(parent)
        AddID()
        MMSetDefaultProperties()
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramItem)
        MyBase.New(prototype)
        AddID()
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramNode)
        MyBase.New(prototype)
        AddID()
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public Property HasRTFnote() As Boolean
        Get
            Return MbHasRTFnote
        End Get
        Set(ByVal IbValue As Boolean)
            MbHasRTFnote = IbValue
        End Set
    End Property
    Public ReadOnly Property ID() As Integer
        Get
            Return MiID
        End Get
    End Property
#End Region
#End Region
#Region "Subs"
#Region "Overrides & Overloads Subs"
    'overrided sub for saving to XML
    Protected Overrides Sub SaveToXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.SaveToXml(xmlElement, context)
        context.WriteString(CStr(MiID), "ID", xmlElement)
        context.WriteString(IIf(MbHasRTFnote, "TRUE", "FALSE"), "HasRTFnote", xmlElement)
    End Sub
    'overrided sub for loading from XML
    Protected Overrides Sub LoadFromXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.LoadFromXml(xmlElement, context)
        MiID = CInt(context.ReadString("ID", xmlElement))
        MbHasRTFnote = IIf((context.ReadString("HasRTFnote", xmlElement)).ToUpper = "TRUE", True, False)
    End Sub
    'overrided sub for drawing a container
    Public Overrides Sub Draw(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal options As MindFusion.Diagramming.RenderOptions)
        MyBase.Draw(graphics, options)

        'drawing triangle for container note indication
        If MbHasRTFnote Then DrawNoteTriangle(Me.Bounds, graphics)
    End Sub
    'overrided sub for drawing handles
    Public Overrides Sub DrawHandles(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal color As System.Drawing.Color)
        'Const LdbWidthOfLine As Double = 0.7
        Dim LoColor As Color

        If Me.Parent.ActiveItem IsNot Nothing Then
            If Me.Parent.ActiveItem.Equals(Me) Then
                'me is active selected
                'Debug.Print("DrawHandles-SELECTED")
                LoColor = Drawing.Color.Blue
            Else
                'me is not active selected
                'Debug.Print("DrawHandles-NOT SELECTED")
                LoColor = Drawing.Color.Gray
            End If
        Else
            'nothing is selected
            'Debug.Print("DrawHandles-NOTHING IS SELECTED")
            LoColor = Drawing.Color.Gray
        End If

        MyBase.DrawHandles(graphics, LoColor)

        ''drawing selection rectangle
        'graphics.DrawRectangle(SDPenVirginCreator(LoColor, LdbWidthOfLine), CType(Me.Bounds.X - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Y - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Width + LdbWidthOfLine, Single), CType(Me.Bounds.Height + LdbWidthOfLine, Single))
    End Sub
    'overrided sub called after deserialization of node. I can set some properties forcible here!
    Protected Overrides Sub OnLoad()
        MyBase.OnLoad()
    End Sub
#End Region
#Region "MM Subs"
    'adds ID for this node. ID identifies this node - useful during synchroselection in compare version mode
    Private Sub AddID()
        Dim LoRandomGenerator As Random = New Random(Now.Millisecond)
        MiID = LoRandomGenerator.Next
    End Sub
    'default settings of shape
    Public Sub MMSetDefaultProperties()
        Me.EnabledHandles = AdjustmentHandles.All - AdjustmentHandles.Rotate
        Me.MinimumSize = New SizeF(20, 20) 'Constraints.MinWidth and Constraints.MinHeight dont work! :-)
        Me.Obstacle = True ' whether this node is considered an obstacle by the link-routing algorithm
        Me.Bounds = SDRectangleVirginCreator(0, 0, 50, 50)
        Me.Font = New Font("Arial", 8, FontStyle.Regular) 'font used to render the node's text
        Me.AllowIncomingLinks = True 'true - because of notes
        Me.AllowOutgoingLinks = True 'true - because of notes
        Me.Brush = New LinearGradientBrush(Color.White, Color.LightGray, 90) 'brush is used to fill the interiors of geometric shapes
        Me.Pen = New Pen("0/#FF696969/0/0/0//0/0/10/") 'pen is used to draw the outline of geometric shapes
        Me.HandlesStyle = HandlesStyle.SquareHandles2  'visual styles for frames that are drawn around selected nodes
        Me.ShadowOffsetX = 0.5 'horizontal offset of the shadow
        Me.ShadowOffsetY = 0.5 'vertical offset of the shadow
        Me.CaptionHeight = 6 'height of the container's caption area
        Me.Margin = 2 'how much space should be left between contained nodes and the container borders
        Me.AutoShrink = False 'whether the container should shrink in size when nodes are removed from it
        Me.Foldable = False 'whether the user is allowed to fold this container
        Me.HighlightPen = New Pen(Color.BlueViolet) 'pen used to draw the container frame lines when an item is dragged over the container
        Me.CaptionFormat.Alignment = StringAlignment.Center  'specifies the alignment of an object relative to its container
        Me.CaptionFormat.FormatFlags = StringFormatFlags.NoClip
        Me.CaptionFormat.Trimming = StringTrimming.None
        Me.CaptionFormat.LineAlignment = StringAlignment.Center
        Me.Caption = GsStringDefaultValue 'the text displayed as caption of the container
    End Sub
    'resizes height of caption and bounds
    Public Sub MMResizeAllToFitContainer()
        MMResizeHeightOfCaption()
        UpdateBounds()
    End Sub
    'resizes heights of caption
    Public Sub MMResizeHeightOfCaption()
        Dim LnNewHeight As Single
        Dim LnWidth As Single

        'checking Me.Parent for MeasureUnit
        If Me.Parent Is Nothing Then
            Debug.Print("CHYBA!!! - MMResizeHeightOfCaption - Me.Parent = Nothing")
        End If

        'resizing height of caption
        'maxWidth parameter for MeasureString sub = actual width of caption
        LnWidth = Me.Bounds.Width

        LnNewHeight = Me.Parent.MeasureString(Me.Caption, Me.Font, LnWidth, Me.CaptionFormat).Height

        'setting a new height or keeping minimal height of caption
        Const LnMinimalCaptionHeight As Single = 6
        Me.CaptionHeight = IIf(LnNewHeight > LnMinimalCaptionHeight, LnNewHeight, LnMinimalCaptionHeight)

        'height of container will be at minimum as height of caption + any constant (10 :-))
        Me.Constraints.MinHeight = Me.CaptionHeight + 10
        'need to set new height right now?
        If Me.Bounds.Height < Me.Constraints.MinHeight Then
            Me.Resize(Me.Bounds.Width, Me.Constraints.MinHeight)
        End If
    End Sub
    'routes links (depends on parameters - all incoming or all outgoing)
    Public Sub MMRouteLinks(Optional ByVal IbRouteIncomingLinks As Boolean = True, Optional ByVal IbRouteOutgoingLinks As Boolean = True)
        If IbRouteIncomingLinks Then
            For Each LoLink As DiagramLink In IncomingLinks
                LoLink.Route()
            Next
        End If

        If IbRouteOutgoingLinks Then
            For Each LoLink As DiagramLink In OutgoingLinks
                LoLink.Route()
            Next
        End If
    End Sub
    'adding node programmatically to container
    Public Sub MMAddNodeToContainerProgrammatically(ByVal OoNode As DiagramNode)
        'add node to container
        Me.Add(OoNode)
    End Sub
#End Region
#Region "Functions"
#Region "MM Functions"
#End Region
#End Region
#End Region



End Class
